<html>
<head><meta charset="utf-8"><title>Inference with integer variables · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html">Inference with integer variables</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="183873740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183873740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183873740">(Dec 19 2019 at 18:53)</a>:</h4>
<p>This came up when implementing <code>Index</code> support in rust-analyzer: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f3f417b1731dcd6e09970651d8f5f084" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f3f417b1731dcd6e09970651d8f5f084">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f3f417b1731dcd6e09970651d8f5f084</a></p>
<p>It seems that rustc detects that since the parameter type is some kind of integer, only the <code>u32</code> impl can apply, and it infers that type. This won't be possible with current Chalk, since it has no concept of integer variables, right?</p>



<a name="183876023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876023">(Dec 19 2019 at 19:18)</a>:</h4>
<p>Yes and no</p>



<a name="183876043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876043">(Dec 19 2019 at 19:18)</a>:</h4>
<p>I've been wondering about this as I started to draft some notes for the "type library"</p>



<a name="183876089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876089">(Dec 19 2019 at 19:19)</a>:</h4>
<p><em>Probably</em> the easiest thing to do is indeed to extend with a "kind" of type variable that can only be an integral type, but I was wondering if we could model it via predicates too</p>



<a name="183876109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876109">(Dec 19 2019 at 19:19)</a>:</h4>
<p>(e.g., some kind of <code>IsIntegral(T)</code> predicate that is true for <code>u32</code> and friends)</p>



<a name="183876146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876146">(Dec 19 2019 at 19:19)</a>:</h4>
<p>It's a bit annoying since I don't really want to make integral types so special but there might be nothing for it</p>



<a name="183876210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183876210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183876210">(Dec 19 2019 at 19:20)</a>:</h4>
<p>(There is also an analogous situation around floating point types; we have special variables for them in rustc too)</p>



<a name="183878258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183878258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183878258">(Dec 19 2019 at 19:43)</a>:</h4>
<p>I think the problem with just having a predicate would be that Chalk would have to iterate through all impls to find potentially matching ones</p>



<a name="183878283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183878283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183878283">(Dec 19 2019 at 19:43)</a>:</h4>
<p>if it knows the set of possible types, it could specifically look for them</p>



<a name="183878302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183878302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183878302">(Dec 19 2019 at 19:43)</a>:</h4>
<p>although I guess the impl search could also be extended to know about the predicates</p>



<a name="183889927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183889927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183889927">(Dec 19 2019 at 22:04)</a>:</h4>
<p>Certainly having variables that cannot be unified with non-integral types would let us reject faster; the compiler still (I think) iterates through all possible impls in this case, but it is able to reject them relatively quickly.</p>



<a name="183889946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Inference%20with%20integer%20variables/near/183889946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Inference.20with.20integer.20variables.html#183889946">(Dec 19 2019 at 22:04)</a>:</h4>
<p>I'm wondering if there is some suitable "generalization" of this concept</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>